#!/bin/bash

if [ $# -ne 1 ]
then
	echo "liwe_install_db (db_name)"
	exit 1
fi

dbname=$1

tgts=$(find . -type d -or -type l)
for t in $tgts
do
	t=$(basename $t)
	if [ ! -e $t/work/$t.sql ]; then continue; fi

	echo -n "Install module '$t' [y/n] ? "
	again=1
	while [ $again -eq 1 ]
	do
		read choice
		case $choice in
			"Y" | "y" )
				skip=0
				again=0
				;;
			"N" | "n" )
				skip=1
				again=0
				;;
			*)
				again=1
				;;
		esac
	done

	if [ $skip -eq 0 ]
	then
		sql=$t/work/$t.sql
		echo "Executing '$sql' in $dbname..."
		mysql -uroot $dbname < $sql
	fi
done
